 /*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 /**
  ******************************************************************************
  * @file    serial_task.c
  * @author  GEEKROS,  site:www.geekros.com
  ******************************************************************************
 */

#include "serial_task.h"

static Serial_Data_Struct Read_Data;

uint32_t Serial_Task_Buffer_Len = 512;

uint8_t Serial_Task_Buffer[512];

#define PI 3.1415926535f
extern Mpu_Calc_Struct Mpu_Calc;
static int angle_buff[3] = {0};

#define FLOAT_0_5  0.5
#define FILTER_TIME 3

#define ADJ_MAG_MAX (500)
#define ADJ_MAG_MIN (-500)

#define T_TIMER_1000MS 1000

#define KEY_SHORT_TIME 3
#define BEEP_FREQ_LOW  100
#define BEEP_FREQ_HIGH 300

/******************************************************************
  * @ 函数名  ： Serial_Task
  * @ 功能说明： USB虚拟串口任务函数
  * @ 参数    ： NULL
  * @ 返回值  ： NULL
  *****************************************************************/
void Serial_Task(void)
{
    bool Calibrate = 0;
    uint8_t key_flag = 0;
    int32_t Max_Mag_x,Max_Mag_y;
    int32_t Min_Mag_x,Min_Mag_y;
    int32_t Mag_x_OffSet = 0,Mag_y_OffSet = 0;
    while(1) {
        uint16_t angle = 0;
        char buff[20] = {0};

        angle_buff[0] = angle_buff[1];
        angle_buff[1] = angle_buff[2];
        angle_buff[2] = ((uint16_t)(atan2((Mpu_Calc.mag_y - Mag_y_OffSet),(Mpu_Calc.mag_x - Mag_x_OffSet)) *180 / PI + 180 ));

        angle = ((uint16_t)((angle_buff[0] + angle_buff[1] + angle_buff[2]) / FILTER_TIME + FLOAT_0_5));

        if(Led_GetStatu("green") == LED_ON) {
            BLUE_LED_TOGGLE();
            memset(buff, 0x00, sizeof(buff));
            sprintf(buff, "angle:%d", angle);
            ESP8266_send_data(buff, strlen(buff));
        } else {
            BLUE_LED_OFF();
            RED_LED_TOGGLE();
        }

        if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2) == SET) {
            key_flag++;           
        } else {
            if(key_flag>=KEY_SHORT_TIME) {
                Max_Mag_x = ADJ_MAG_MIN;
                Max_Mag_y = ADJ_MAG_MIN;
                Min_Mag_x = ADJ_MAG_MAX;
                Min_Mag_y = ADJ_MAG_MAX;

                TIM_SetCompare1(TIM12, BEEP_FREQ_LOW);
                delay_ms(T_TIMER_1000MS);
                TIM_SetCompare1(TIM12, BEEP_FREQ_HIGH);
                Calibrate = !Calibrate;
            }
            key_flag = 0;
        }

        if(Calibrate) {
            if(Max_Mag_x < Mpu_Calc.mag_x) {
                Max_Mag_x = Mpu_Calc.mag_x;
            }
            if(Max_Mag_y < Mpu_Calc.mag_y) {
                Max_Mag_y = Mpu_Calc.mag_y;
            }
            if(Min_Mag_x > Mpu_Calc.mag_x) {
                Min_Mag_x = Mpu_Calc.mag_x;
            }
            if(Min_Mag_y > Mpu_Calc.mag_y) {
                Min_Mag_y = Mpu_Calc.mag_y;
            }
            Mag_x_OffSet = (Max_Mag_x + Min_Mag_x) / 2;  //取平均
            Mag_y_OffSet = (Max_Mag_y + Min_Mag_y) / 2;  //取平均
        }
        Task_Delay(5);
    }
}
